﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NReader;
using NReader.Service;

namespace XnReader.WinRT.Helper
{
    /// <summary>
    /// 章节列表分页
    /// </summary>
    public class ChapterListPage<T> : IncrementalLoadingBase
    {
        public ChapterListPage(string webflag, uint maxCount, uint articleId)
        {
            _webflag = webflag;
            _maxCount = maxCount;
            _articleId = articleId;
        }

        protected override async Task<IList<object>> LoadMoreItemsOverrideAsync(System.Threading.CancellationToken c,
                                                                                uint count)
        {
            uint toGenerate = System.Math.Min(count, _maxCount - _count);

            // Wait for work 
            await Task.Delay(10);

            var values =
                await
                ChapterService.GetBookReaderByWeb(_webflag, (int) _articleId, (int) _count, (int) toGenerate);
            _count += toGenerate;
            return values.ToArray();
        }

        protected override bool HasMoreItemsOverride()
        {
            return _count < _maxCount;
        }

        #region State

        private uint _count = 0;
        private uint _maxCount;
        private string _webflag;
        private uint _articleId = 0;

        #endregion
    }
}
